package metadata_tab

import (
	"file_explorer/common/log"
	"file_explorer/model"
	"file_explorer/service/eventbus"
	"file_explorer/view/components/main_panel/fe_tab"
	"file_explorer/view/store"

	"fyne.io/fyne/v2/container"
	"fyne.io/fyne/v2/widget"
)

type MetadataTab struct {
	fe_tab.DefaultTab

	editForm  *MetadataEditForm
	toolbar   *MetadataTabToolbar
	preview   *MetadataPreview
	statusBar *StatusBar

	tabCtx *store.MetadataTabContext
}

func NewEditMetadataTab(tabCtx store.TabContext) *MetadataTab {
	tab := &MetadataTab{
		tabCtx: tabCtx.(*store.MetadataTabContext),
	}

	// 创建子组件
	tab.preview = NewMetadataPreview(tab.tabCtx)
	tab.editForm = NewMetadataEditForm(tab.tabCtx, tab.preview)
	tab.toolbar = NewToolbar(tab.preview)
	tab.statusBar = NewStatusBar(tab.tabCtx)

	// preview内选中文件改变时回调
	tab.preview.Checklist.RegisterConsumer(eventbus.EventKeyWidgetChecklistChange, func(event *model.Event) {
		log.Info("元数据页选中项变更，路径：%s", tab.tabCtx.Path)
		// 选中项更新时刷新状态栏
		tab.statusBar.refreshStatus()
	})

	// 初始状态刷新
	tab.preview.resetCheckItems()

	tab.Container = container.NewBorder(
		container.NewVBox(
			widget.NewLabel("1. \"*\"表示修改前内容; 2. 回车添加标签，双击移除标签；3. 选中项可修改，提交时包含所有项"),
			tab.editForm.Form,
			tab.toolbar.Container,
		), tab.statusBar.Label, nil, nil, tab.preview.Table,
	)

	return tab
}
